package com.sgb.kjwl.view.ui.activity.transport;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityGoodsCarsBinding;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.constant.RouterKey;
import com.sgb.kjwl.view.ui.fragment.home.FindCarMapAndListFragment;
import com.sgb.kjwl.view.ui.fragment.transport.GoodsHallListFragment;
import com.sgb.kjwl.view.ui.fragment.transport.MapCarsFragment;
import com.sgb.kjwl.view.ui.fragment.transport.ProfessionalServicesFragment;
import com.sgb.kjwl.view.widget.transport.LogisticsWorkbench;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;

import java.util.ArrayList;

public class GoodsCarsActivity extends AppActivity {

    ActivityGoodsCarsBinding binding;
    public static final int FRAGMENT_GOOS = 0;
    public static final int FRAGMENT_CARS = 1;
    private FragPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
//    String[] titles = {"货运大厅", "货主询价"};
    String[] titles = {"货运大厅"};
    String[] carTitles={"普通货车","司机找货"};

    public static final int GOODS = 0;
    public static final int CARS = 1;
    public int index = 0;
    public int state;
    private boolean canShowListView = true;//是否列表模式
    //二级页面no（货物类型no/车型no）
    private String mNo;

    /**
     * @param index 货运大厅:GOODS     车辆大厅:CARS
     */
    public static void start(Context context, int index, boolean car) {
        start(context, index, car, "");
    }

    public static void start(Context context, int index, boolean car, String no) {
        Intent intent = new Intent();
        intent.putExtra("index", index);
        intent.putExtra("isCar", car);
        intent.putExtra(RouterKey.NO, no);
        intent.setClass(context, GoodsCarsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context, int index, boolean car, boolean canShowListView) {
        Intent intent = new Intent();
        intent.putExtra("index", index);
        intent.putExtra("isCar", car);
        intent.putExtra("canShowListView", canShowListView);
        intent.setClass(context, GoodsCarsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    public static void start(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, GoodsCarsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_goods_cars);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        binding.rightView.setOnClickListener((View) -> {
            boolean isCar = getIntent().getBooleanExtra("isCar", false);
            if (!isCar) {
                //地图
                MapGoodsAndCarsActivity.start(this, 0);
            } else {
                MapGoodsAndCarsActivity.start(this, 1);
            }
        });
        DialogHelper.dismissProgressDialog();
    }

    @Override
    protected void init() {
        initBundleParams();
        boolean isCar = getIntent().getBooleanExtra("isCar", false);
        mFragments = new ArrayList<>();
        if(!isCar) {
         /*   RelativeLayout.LayoutParams rl= (RelativeLayout.LayoutParams) binding.tablayout.getLayoutParams();
            rl.addRule(RelativeLayout.RIGHT_OF, R.id.iv_back);
            binding.tablayout.setLayoutParams(rl);*/
            //货运大厅
            if (index == 0 && !TextUtils.isEmpty(mNo)) {
                mFragments.add(GoodsHallListFragment.newInstance(0, true, true, canShowListView, !canShowListView, mNo));
            } else {
                mFragments.add(GoodsHallListFragment.newInstance(0, true, true, canShowListView, !canShowListView));
            }
//            if (index == 1 && !TextUtils.isEmpty(mNo)) {
//                mFragments.add(GoodsHallListFragment.newInstance(1, true, true, canShowListView, !canShowListView, mNo));
//            } else {
//                mFragments.add(GoodsHallListFragment.newInstance(1, true, true, canShowListView, !canShowListView));
//            }
            //询价货运
//            mFragments.add(ProfessionalServicesFragment.newInstance());
            binding.tablayout.setIndicatorHeight(0f);
            pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(titles.length);
            binding.tablayout.setViewPager(binding.viewpager, titles);
        }else{
            if (index == 0 && !TextUtils.isEmpty(mNo)) {
                mFragments.add(MapCarsFragment.newInstance(canShowListView, mNo));
            } else {
                mFragments.add(MapCarsFragment.newInstance(canShowListView));
            }
            if (index == 1 && !TextUtils.isEmpty(mNo)) {
                mFragments.add(FindCarMapAndListFragment.newInstance(mNo, canShowListView));
            } else {
                mFragments.add(FindCarMapAndListFragment.newInstance("", canShowListView));
            }
            pagerAdapter = new FragPagerAdapter(getSupportFragmentManager(), mFragments);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setOffscreenPageLimit(carTitles.length);
            binding.tablayout.setViewPager(binding.viewpager, carTitles);
        }


        binding.viewpager.setCurrentItem(index);
        if(UserOperating.getInstance().isLogged()) {
            LogisticsWorkbench.newInstance().attach(this);
        }
    }

    private void initBundleParams() {
        if (getIntent() != null) {
            index = getIntent().getIntExtra("index", 0);
            canShowListView = getIntent().getBooleanExtra("canShowListView", true);
            mNo = getIntent().getStringExtra(RouterKey.NO);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0 && resultCode == 1) {
            state = data.getIntExtra("state", -1);
        }
    }
}